<template>
  <div>
    <el-upload
      :class="{disable: fileList.length === 1}"
      list-type="picture-card"
      action="#"
      :file-list="fileList"
      :on-preview="preview"
      :on-remove="onRemove"
      :on-change="onChange"
      :before-upload="beforeUpload"
      :http-request="upload"
      :limit="1"
    >
      <i class="el-icon-plus" />
    </el-upload>

    <el-progress v-show="showProgress" :percentage="percent" :show-text="false" class="progress" />

    <el-dialog title="浏览头像" :visible.sync="showDialog">
      <el-row type="flex" justify="center">
        <img :src="imgUrl" alt="">
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
var COS = require('cos-js-sdk-v5')
var cos = new COS({
  SecretId: 'AKID78tOcZEcjZ8QRCgtpUt62h5C2btue7IC',
  SecretKey: 'e17wVmnXzA4h4B1buvf9MzLQEl1zg6F6'
})
export default {
  data() {
    return {
      fileList: [], // 图片地址设置为数组
      showDialog: false, // 控制显示弹层
      imgUrl: '',
      percent: 0,
      showProgress: false
    }
  },
  methods: {
    //   点击文件列表中已上传的文件时的钩子
    preview(file) {
      // 这里应该弹出一个层 层里是点击的图片地址
      this.imgUrl = file.url
      this.showDialog = true
    },
    // 文件列表移除文件时的钩子
    onRemove(file, fileList) {
    //   console.log(fileList)
      this.fileList = [...fileList]
    },
    // 文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
    onChange(file, fileList) {
    //   console.log('onChange:', file, fileList)
      this.fileList = [...fileList]
    },
    // 上传文件之前的钩子，参数为上传的文件，若返回 false 或者返回 Promise 且被 reject，则停止上传。
    beforeUpload(file) {
    //   console.log('beforeUpload:', file)
      //   限制上传大小
      const maxSize = 500 * 1024
      if (file.size > maxSize) {
        this.$message.warning('文件大小不能超过 500k')
        return false
      }
      //   限制上传格式
      const whiteList = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      if (!whiteList.includes(file.type)) {
        this.$message.warning('目前只允许 jpeg / png 图片')
        return false
      }
    },
    // 覆盖默认的上传行为，可以自定义上传的实现
    upload(params) {
      console.log('开始上传')
      console.log(params)
      cos.putObject({
        Bucket: '1-1306731821', /* 必须 */
        Region: 'ap-guangzhou', /* 存储桶所在地域，必须字段 */
        Key: params.file.name, /* 必须 */
        StorageClass: 'STANDARD',
        Body: params.file, // 上传文件对象
        onProgress: (progressData) => {
        //   console.log(JSON.stringify(progressData))
          this.showProgress = true
          this.percent = progressData.percent * 100
        }
      }, (err, data) => {
        // console.log(err || data)
        if (!err && data.statusCode === 200) {
        //   console.log(this.fileList)
          this.fileList[0].url = 'http://' + data.Location
          this.fileList[0].status = 'success'
        }

        setTimeout(() => {
          this.showProgress = false
        }, 800)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.disable {
  ::v-deep .el-upload--picture-card {
    display: none;
  }
}
.progress {
    width: 146px;
}
</style>
